package com.platform.upms.service.impl;

import com.platform.common.base.impl.BaseServiceImpl;
import com.platform.common.page.Page;
import com.platform.upms.dao.UpmsOrgDao;
import com.platform.upms.dao.UpmsOrgMenuDao;
import com.platform.upms.dao.UpmsPositionDao;
import com.platform.upms.model.UpmsOrg;
import com.platform.upms.model.ZNode;
import com.platform.upms.model.json.Tip;
import com.platform.upms.service.UpmsOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UpmsOrgServiceImpl extends BaseServiceImpl<UpmsOrg> implements UpmsOrgService {

    @Autowired
    private UpmsOrgDao upmsOrgDao;

    @Autowired
    private UpmsPositionDao upmsPositionDao;

    @Autowired
    private UpmsOrgMenuDao upmsOrgMenuDao;

    /**
     * 保存数据
     * @param upmsOrg
     * @return
     */
    public Integer insert(UpmsOrg upmsOrg)  {
        return upmsOrgDao.insert(upmsOrg);
    }

    /**
     * 根据Id删除
     * @param id
     * @return
     */
    public Integer deleteById(String id) {
        return upmsOrgDao.deleteById(id);
    }

    /**
     * 根据id修改
     * @param upmsOrg
     * @return
     */
    public Integer update(UpmsOrg upmsOrg) {
        return upmsOrgDao.update(upmsOrg);
    }

    /**
     * 查询列表
     * @param upmsOrg
     * @return
     */
    @Override
    public List<UpmsOrg> selectAllList(UpmsOrg upmsOrg) {
        return upmsOrgDao.selectAllList(upmsOrg);
    }

    @Override
    public UpmsOrg getOrg() {
        UpmsOrg upmsOrg = upmsOrgDao.getOrg();
        return upmsOrg;
    }

    @Override
    public List<ZNode> listOrg(Integer id) {
        List<ZNode> list = upmsOrgDao.listOrg(id);
        return list;
    }

    @Override
    public Tip judgeAnddelete(Integer[] ids) {
        Tip tip = new Tip("删除成功");
        Integer orgs = upmsOrgDao.findByOrgIds(ids);
        if(orgs == 0) {
            Integer positions = upmsPositionDao.findByOrgIds(ids);
            if(positions == 0) {
                //删除组织所授权限
                upmsOrgMenuDao.deleteByOrgIds(ids);
                //删除组织
                upmsOrgDao.deleteDataById(ids);
            } else {
                tip = new Tip(2, "请先删除组织下的职务");
            }
        } else {
            tip = new Tip(1, "请先删除下级组织");
        }
        return tip;

    }

    @Override
    public Integer saveOrg(UpmsOrg upmsOrg) {
        Integer flag = 0;
        try {
            upmsOrgDao.saveOrg(upmsOrg);
            upmsOrgDao.editOrgIdById(upmsOrg.getId());
            flag = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return flag;
    }

    @Override
    public Page<UpmsOrg> getHighOrgList(Page<UpmsOrg> page, UpmsOrg org) {
        page.setResults(upmsOrgDao.getHighOrgList(page, org));
        return page;
    }

    @Override
    public Integer editOrgIdById(Integer id) {
        return upmsOrgDao.editOrgIdById(id);
    }


}
